package com.byls.controller;

import com.byls.dao.MemberMapper;
import com.byls.pojo.Lawyer;
import com.byls.pojo.Lawyercase;
import com.byls.pojo.Members;
import com.byls.repository.MembersRepository;
import com.byls.util.PageTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.nio.channels.MembershipKey;
import java.util.List;
import java.util.Map;

@RequestMapping("/members")
@RestController
public class MembersController {
    @Autowired
    private MembersRepository membersRepository;
    @Autowired
    private MemberMapper memberMapper;
    @RequestMapping("/findall")
    private List<Members>  getAllMember (){
        return membersRepository.findAll();
    }
    @PutMapping("/update")
    public String update(@RequestBody Members member){
        Members res = membersRepository.save(member);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @PostMapping("/findbyname")
    private Page<Members> findByName(@RequestBody Map map){
        Pageable pageable= PageRequest.of(0,6);
        List<Members>memberList=memberMapper.getMemberByName(map.get("name").toString());
        return PageTest.listConvertToPage(memberList,pageable);
    }
    @GetMapping("/findall/{page}/{size}")
    private Page<Members> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable= PageRequest.of(page-1,size);
        return membersRepository.findAll(pageable);
    }
    @PostMapping("/save")//tong guo RequestBody jiang jsonduixiang zhuanwei java duixiang
    public String save(@RequestBody  Members member){
        Members res = membersRepository.save(member);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        membersRepository.deleteById(id);
    }
}
